package com.multitrack.map;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.facebook.android.CustomFacebook;
import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;
import com.facebook.android.Utility;
import com.multitrack.datatype.Event;
import com.multitrack.main.R;
import com.multitrack.map.datatype.GeoPoints;
import com.multitrack.map.datatype.Polyline;
import com.multitrack.util.CloseAlertDialog;
import com.multitrack.util.SQLite;

public class TrackMapActivity extends BasicMapActivity {

	final String TAG = "TrackMapActivity";

	// SQLite
	private SQLite mHelper;
	private Event myEvent;
	private final String MY_EVENT = "myEvent";

	// Map Draw

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// SQLite
		mHelper = new SQLite(this);

		if (mHelper.dbSelectMyEvent() == null) {
			newMyEvent();
		} else {
			newMyEventNonNormal();
		}

		// Line
	}

	private void newMyEventNonNormal() {
		OnClickListener pListener = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				myEvent = mHelper.dbSelectMyEvent();
				GeoPoints tracks = mHelper.dbGetMyTracks();
				if (tracks.size() == 0) {
					return;
				}
				lines = new Polyline(tracks);
				mCustomMap.getOverlays().add(lines);

				beginMarker.draw(tracks.get(0));
				mCustomMap.invalidate();
			}

		};
		OnClickListener nListener = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mHelper.dbDeleteEvent(MY_EVENT);
				newMyEvent();
			}
		};
		new CloseAlertDialog(this, R.string.notice,
				"비정상적인 종료가 있었습니다.\n이전에 기록을 불러오시겠습니까?", pListener, nListener)
				.show();
	}

	private void newMyEvent() {
		myEvent = new Event();
		myEvent.setId(mHelper.dbGetCurrTime());
		myEvent.setName(MY_EVENT);
		myEvent.setLeaderId(mHelper.dbGetMyId());

		mHelper.dbInsertEvent(myEvent);
		mHelper.dbInsertEntrant(myEvent.getId(), myEvent.getLeaderId());
	}

	// 캡쳐하기 부분 시작

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		CustomFacebook cf = new CustomFacebook(this);
		switch (item.getItemId()) {

		case R.id.logout:
			cf.logout();
			break;

		case R.id.login:
			cf.login();
			break;

		case R.id.capture:
			cf.screenCapture(mCustomMap);
			break;
		}

		return super.onOptionsItemSelected(item);

	}

	final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {

		/*
		 * if this is the activity result from authorization flow, do a call
		 * back to authorizeCallback Source Tag: login_tag
		 */
		case AUTHORIZE_ACTIVITY_RESULT_CODE:

			CustomFacebook cf = new CustomFacebook(this);
			cf.screenCapture(mCustomMap);
			break;

		}
	}

	public void onDestroy() {
		super.onDestroy();
		mHelper.close();
	}

	@Override
	public void onShowDialog() {
		// TODO Auto-generated method stub
	}

	// 위치가 변경되면 자동 불러오는 메소드
	public void onLocationChanged(Location location) {
		switch (getCtrlState()) {

		case PLAY:
			mHelper.dbInsertTrack(myEvent.getId(), location);
			super.drawLine(location);
			break;

		case PAUSE:
			break;

		case STOP:
			// 정장 및 선 삭제
			// 저장 여부
			// 예 -> 저장 후 맵 클리어
			// 아니오 -> 취소
			
			
			break;
		}
	}

	@Override
	public void onKeyUpBack() {
		OnClickListener listener = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mHelper.dbDeleteEvent(myEvent.getId());
				finish();
			}
		};
		new CloseAlertDialog(this, R.string.notice,
				"정말 나가시겠습니까?\n(R.string 등록)", listener, null).show();
	}
	
	@Override
	public void mOnClick(View v) {
		switch(v.getId()) {
		case R.id.imgStop:
			// 선 삭제
			mCustomMap.getOverlays().remove(lines);
			// 시작 마커 삭제
			beginMarker.clear();
			// SQLite 트랙 정보 삭제
			mHelper.dbDeleteTrack(MY_EVENT);			
			break;
		}
		
		super.mOnClick(v);
	}
	
}
